package tests;

import java.io.*;

import iterator.*;

import iterator.CondExpr;

import iterator.FldSpec;

import iterator.RelSpec;

import java.io.*;

import java.util.*;
import java.lang.*;
import heap.*;
import bufmgr.*;
import diskmgr.*;
import global.*;
import chainexception.*;

import columnar.*;

public class IndexChoice implements GlobalConst {

	/*
	 * constructor for all parameters - datafilename , coulmnDBname, columnar
	 * filename , numcolumns
	 */
	public String datafilename, cdbname, filename;
	public int numcolumns;
	public TID[] tidArr;
	ColumnarFile _cf;
	int[] colnosg;
	int[] attrtypesg;

	public IndexChoice() {
	}

	public void indexTest(String dbname, String ColfileName, String colname,
			String indexType) throws HFException, HFBufMgrException,
			HFDiskMgrException, IOException {
		try {
			PCounter.initialize();
			String dbpath = "/tmp/" + dbname;
			String record;

			SystemDefs sysdef = new SystemDefs(dbpath, 0, 700, "Clock");
			int i = 0, j = 0;

			BufferedReader br = new BufferedReader(new FileReader("/tmp/"
					+ ColfileName + ".hdr"));

			while ((record = br.readLine()) != null) {
				j++;
			}
			// all colnames, colnumbers and attr by parsing the name.hdr
			String[] colnames = new String[j];
			int[] colnos = new int[j];
			int[] attrtypes = new int[j];
			AttrType[] attr = new AttrType[j];
			// constraint colname,attr,number
			int condn_columnno = -1, condn_attrType = -1;
			String condn_columnname = " ";
			// target column number and attr
			// int[] final_colnums= new int[targetcolname.length];
			// int[] final_attr= new int[targetcolname.length];
			String[] targetarray = new String[10];
			// read from name.hdr

			BufferedReader br4 = new BufferedReader(new FileReader("/tmp/"
					+ ColfileName + ".hdr"));

			while ((record = br4.readLine()) != null) {

				StringTokenizer st4 = new StringTokenizer(record, "\t");

				while (st4.hasMoreTokens()) {
					StringTokenizer st5 = new StringTokenizer(st4.nextToken(),
							" ");
					String colName = st5.nextToken().trim();
					colnames[i] = colName;

					int colNum = Integer.parseInt(st5.nextToken().trim());
					colnos[i] = colNum;
					int attrType = Integer.parseInt(st5.nextToken().trim());
					attrtypes[i] = attrType;
					if (attrType == 0)
						attr[i] = new AttrType(AttrType.attrString);
					else
						attr[i] = new AttrType(AttrType.attrInteger);

					if (colName.equals(colname)) {
						condn_columnno = colNum;
						condn_attrType = attrType;
						// condn_columnname=colName;

						System.out.println(condn_columnno);

						// break;

					}

				}

				i++;

			}
			_cf = new ColumnarFile(ColfileName, i, attr);
			if (indexType.equalsIgnoreCase("BTree")) {
				_cf.createBTreeIndex(ColfileName, condn_columnno,
						condn_attrType);
			}
			else if(indexType.equalsIgnoreCase("Bitmap")){
			    _cf.findDistinctValues(condn_columnno, condn_attrType);	
			    }else{
			    System.out.println("Invalid Index Types");	
			    }
		} catch (Exception e) {
		} finally {
			try {
				// IntegerValue vc = new IntegerValue(new Integer(1));
				// cf.createBitMapIndex(2, (ValueClass)vc);
				// for(i=0;i<this.tidArr.length;i++)
				// cf.getTuple(this.tidArr[i]);
				// cf.markTupleDeleted(tid);
				// cf.purgeAllDeletedTuples();
				SystemDefs.JavabaseBM.unpinAllPages();
				SystemDefs.JavabaseBM.flushAllPages();
				System.out.println("Number of Pages Read: " + PCounter.rcounter);
				System.out.println("Number of Pages Writes: "
						+ PCounter.wcounter);
			} catch (Exception e) {
			}
		}

	}

	public static void main(String args[]) throws IOException, HFException,
			HFBufMgrException, HFDiskMgrException {

		IndexChoice ic = new IndexChoice();
		System.out.println("Enter the Column name for Btree/Bitmap Construction");
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		System.out.println("Enter the IndexType(Bitmap/BTree): ");
		String acc = sc.nextLine();
		ic.indexTest("cdbname", "hpfile", s, acc);

	}
}